
package Cliente.Vista;

import Cliente.Controlador.Exceptions;
import Cliente.Controlador.GameController;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * Edits an user
 * @author Angel Enrique
 */
public class EditPlayer extends javax.swing.JInternalFrame {

    Properties props = NewPlayer.getProps();
    GameController game = null;
    private String id = null;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }  
    
    public EditPlayer(GameController game) {
       
        this.game = game;
        putDates();
    }

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {                                            
      
        EditPlayer(); 
    }                                           
  
    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                              
       
       DeletePlayer(); 
    }   
   
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration                   

    /*
     * PutDates method that receives a string arrayList with users. 
     * In each block there is a user arrayList tokenized: 
     * nick # password # mail # typeUser.
     */
    private void putDates() {
        
        ArrayList<String> listaClientes = null;
        try {
            listaClientes = game.getUsers();
        } catch (Exceptions ex) {
            JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));             
        }
        Iterator<String> iter = listaClientes.iterator();
        String cliente;
        StringTokenizer str;
        int contador = 0;
         
        while(iter.hasNext()){
            
            cliente = iter.next();  
            String token;
            str = new StringTokenizer(cliente,"#");
            contador = 0;
            int integer = 0;
            
            while(str.hasMoreTokens()){
  
             token = str.nextToken();
             
             if(contador == 0){
               
                jComboBox1.addItem(token);
                jTextArea2.setText(jTextArea2.getText() + Constantes.PLAYER_ID + token + "\n"); 
                setId(token);     
             }
             if(contador == 1){    
            
                jTextArea2.setText(jTextArea2.getText() + Constantes.PLAYER_NICK + token + "\n");
            }
             if(contador == 2){ 
                 
                jTextArea2.setText(jTextArea2.getText() + Constantes.PLAYER_EMAIL + token + "\n");
            }if(contador == 3){ 
             
                integer = Integer.parseInt(token);
                if(integer == 0){
                    token = Constantes.PLAYER_USER_TYPE_ADMIN;
                }else if(integer == 1){
                    token = Constantes.PLAYER_USER_TYPE_USER;
                }else{
                    token = Constantes.PLAYER_USER_TYPE_NOT_DEFINITE;
                }
                jTextArea2.setText(jTextArea2.getText() + Constantes.PLAYER_USER + token + "\n");
            }
          
            contador++;
            }
             
            jTextArea2.setText(jTextArea2.getText()+ "--------------" + "\n");
            jTextArea2.setLineWrap(true); 
                   
        }      
        
    }  
    public void SelectedItemWarning() {
        JOptionPane.showMessageDialog(null, "No selected item.");
    }
    
    /**
     * DeletePlayer sending the player removes his ID to the controller.
     */
    private void DeletePlayer(){
        
        String str = null;
        str = (String) jComboBox1.getSelectedItem();   
        try {
            game.removeUser(Integer.parseInt(getId()));
        } catch (Exceptions ex) {
          JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));
        }
    }
    
    private void EditPlayer(){
        
        String str = null;
        str = jComboBox1.getSelectedItem().toString();
        try {
            String userString = game.getUser(Integer.parseInt(getId()));
        } catch (Exceptions ex) {
          JOptionPane.showMessageDialog(null,props.getProperty(ex.getMessage()));     
        }
        
        NewPlayer player = new NewPlayer(game,str);    
    }
}
